package com.imooc.juc.bean;

/**
 * @Date:2021/11/1/21:08
 * @Description: 这个资源类对应 ProcomsumerDemo 类操作，主要用 synchronized 来锁定 this 对象
 */
public class SaleTicket {
    private  int number = 0;

    public synchronized void increment() throws InterruptedException {

        //1 判断
//        if( number != 0){
//
//            this.wait();
//        }
        while ( number !=0 ) {
            this.wait();
        }


//      //2 干活
        ++number;
        System.out.println(Thread.currentThread().getName() + "\t" +number);
        this.notifyAll();

    }

    public synchronized void decrement() throws InterruptedException {
        //1 判断
//        if (number ==0){
//            this.wait();
//        }

        while (number ==0){
            this.wait();
        }
        //2 干活
        --number;
        System.out.println(Thread.currentThread().getName() + "\t"+ number);

        //3 唤醒
        this.notifyAll();

    }

}
